package nels4561;

import spacewar2.objects.Asteroid;
import spacewar2.objects.Base;
import spacewar2.objects.Beacon;
import spacewar2.objects.Ship;
import spacewar2.objects.SpacewarObject;
import spacewar2.simulator.Toroidal2DPhysics;

public class EgocentricRepresentation {
	/**
	 * Weights for target setting function
	 * All arbitrary.
	 * {@value} SHIP_VALUE: set to zero, but could be used in future
	 * {@value} BASE_VALUE: set to zero, but could be used in future
	 */
	static final int BEACON_VALUE = 1000;
	static final int BEACON_THRESHOLD = 2500;
	static final int SHIP_VALUE = 0;
	static final int BASE_VALUE = 0; 
	static final double MONEY_WEIGHT = 3.0;
	static final double BEACON_WEIGHT = .1;
	static final int OUT_OF_TIME = 18800;
	
	/**
	 * Target that the Representation is setting a value for.
	 * */
	SpacewarObject target;
	/**
	 * Value of the target.
	 * Set based on the type of target and the distance from the ship.
	 * */
	double targetValue;
	/**
	 * The distance from the ship to the target.
	 * Used in calculating the Target's relative worth.
	 * */
	double distanceToTarget;
	
	public EgocentricRepresentation(SpacewarObject t, Toroidal2DPhysics space, Ship ship, int time)
	{
		//initialize class variables
		target = t; 
		distanceToTarget = space.findShortestDistance(ship.getPosition(), target.getPosition());
		
		//set targetValue of Beacon SpacewarObject
		if(target instanceof Beacon)
		{
			targetValue = (BEACON_VALUE - BEACON_WEIGHT*ship.getEnergy())/distanceToTarget;
			if(ship.getEnergy()< BEACON_THRESHOLD)
			{
				targetValue = 3*BEACON_VALUE/distanceToTarget;
			}
		}
		//set targetValue of Base SpacewarObject
		else if(target instanceof Base)
		{
			targetValue = (BASE_VALUE + MONEY_WEIGHT*ship.getMoney())/distanceToTarget;
			if(time> OUT_OF_TIME && ship.getMoney()>0 && ship.getEnergy()>1200)
			{
				targetValue = Double.MAX_VALUE;
			}
		}
		//set targetValue of Asteroid SpacewarObject
		else if(target instanceof Asteroid)
		{
			targetValue = MONEY_WEIGHT*((Asteroid) target).getMoney()/distanceToTarget;
		}
		//set targetValue of Ship SpacewarObject
		//this will be used for shooting other ships in future projects
		else if(target instanceof Ship)
		{
			targetValue = SHIP_VALUE/distanceToTarget;
		}
		//if something goes wrong, set targetValue to 0
		else
		{
			targetValue = 0;
		}
	}

}
